package org.pro.View;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.pro.Control.WarControl;
import org.pro.Model.WarData;

public class GameView extends JFrame {
    WarData warData;
    public GamePanel panel = new GamePanel();
    public int width = 860;
    public int height = 640;
    private int _frames = 0;
    private float _dt = 0.0F;
    private float _frameRate = 0.0F;
    public WarControl warControl;
    public JButton startButton;
    public JButton endButton;
    public JButton saveButton;
    public JButton loadButton;
    public GameView(WarData warData) {
        // 初始化
        this.warData = warData;
        this.panel.setWarData(warData);
        this.setSize(this.width, this.height);
        this.setLocationRelativeTo(null);  // 居中显示
        this.setTitle("坦克大战 V1.0");
        this.setVisible(true);

        // 使用 JLayeredPane 以便按钮和面板可以叠加显示
        JLayeredPane layeredPane = new JLayeredPane();
        this.setContentPane(layeredPane);

        // 设置背景面板（GamePanel）
        panel.setBounds(0, 0, this.width, this.height);
        layeredPane.add(panel, Integer.valueOf(0));  // 设置底层为 0


        this.warData.startTime = System.currentTimeMillis(); // 重新记录游戏开始时间
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    public void update(double timeFlaps) {
        ++this._frames;
        this._dt = (float)((double)this._dt + timeFlaps);
        if (this._dt >= 1.0F) {
            this._frameRate = (float)this._frames / this._dt;
            this._frames = 0;
            this._dt = 0.0F;
        }
        this.panel.setFrameRate(this._frameRate);
        this.panel.repaint();
    }

    // GameView.java 中新增方法
    public void updateWarData(WarData newWarData) {
        this.warData = newWarData;
        panel.setWarData(newWarData); // 更新面板的 warData
    }


}